// auth: macro gu
// email & skype: macrogu@hotmail.com


#include <sstream>
#include <cstdlib>
#include <unistd.h>
#include "read_conf.h"
#include "email_sender.h"
#include <thread>
#include <chrono>
#include "db_operator.h"
#include "voice_server.h"
#include "opFastdfs.h"
#include "log.h"


int main(int argc, char **args) {
	using namespace log4cxx;
	// 读取配置文件
	PropertyConfigurator::configure("conf/log.cfg");

	if (!CAllConfigure::GetInstance().hasLoadConfRight()) 
	{
		ERROR_MSG("read configure error !");
		return -1;
	}

	if (!COpFastDfs::GetInstance().IsFastDfsInitializated())
	{
		ERROR_MSG("initializate the fastdfs error! ");
		return -1;
	}

	DEBUG_MSG("main thread id: " << std::this_thread::get_id());


	//  write log unit test
	//  if we can see logDebug log ,in log , as a time file name log file ,it pass testing
	/*
	g_logger.SetLogPath("./log/");
		LogDebug("test","test");
	*/

	// read xml configure unit test
	// if we see all client info print in log file, it pass testing
	/*
	readClientInfo("./conf/clientinfo.xml");
	for (auto testClientInfo_iter = g_AllClients.begin(); testClientInfo_iter != g_AllClients.end();
			testClientInfo_iter++)
	{
		LOG_DEBUG("clientkey: %s, clientEmail:%s, memoryLimit:%f, cpuLimit:%f,processesLimit:%u", 
		testClientInfo_iter->second.clientKey.c_str(),testClientInfo_iter->second.clientEmail.c_str(), testClientInfo_iter->second.memoryLimit ,
		testClientInfo_iter->second.cpuLimit,testClientInfo_iter->second.processesLimit);
	}
	*/

	
	// email sent union test
	// if we receive email in account macrogu@qq.com , you can use your account
	// it pass testing
	/*
	CEmailSender::getInstance()->SendEmail("macrogu@qq.com","union test");
	*/

	// DataBase store union test
	// if we see "union test" is stored in database, it pass testing
	/*
	CDbOperator::getInstance()->InserDataToDB("union test",1,2,3);
	*/

	// network union test
	// to use curl tool in linux, input
	//  curl -d "client_key=client1" "192.168.96.245:1234/login"
	//  if it print {"isLegalClient":0}   client1 is in client.xml
	//  and we can see log in logfile,  it pass unit test
	/*
	HttpServer http_serverTest;
    http_serverTest.add_mapping("/login", login);
	http_serverTest.start_async();
	*/

	if (!CAllConfigure::GetInstance().hasLoadConfRight())
	{
		return -1;
	}

	stServerInfo mServerInfo = CAllConfigure::GetInstance().GetLocalServerConfiguration();
	CVoiceServer voiceServer(mServerInfo.serverIp, mServerInfo.backlog,mServerInfo.maxevents, mServerInfo.port);
	voiceServer.StartServer();

	return 0;
}
